<?php

include_once("class_lib.php");

$tagName = stripslashes($_GET["tag"]);

$ctrl = new controller();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>

</title>

<!-- Meta Tags -->
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
<meta name="robots" content="index, follow" />

<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="author" content="" />

<!-- Favicon -->
<link rel="shortcut icon" href="" />

<!-- CSS -->
<link rel="stylesheet" href="" media="screen,projection" type="text/css" />
<link rel="stylesheet" href="" media="print" type="text/css" />
<style type="text/css">
<!--

/* ----- CSS ----- */

-->
</style>

<!-- RSS -->
<link rel="alternate" href="" title="RSS Feed" type="application/rss+xml" />

<!-- JavaScript : Include and embedded version -->
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="jquery.livequery.js" type="text/javascript"></script>
<script src="jquery.form.js" type="text/javascript"></script>
<script type="text/javascript">
<!--

$(document).ready(function()
{ 
    $("#myForm").ajaxForm({ success: onModifyTag, dataType: "json" });
	
	$("#tagListContent li a.remove").livequery('click', function(event) {
		
		var name = $(this).attr("href").split("/").pop();
		var tag = "<?= $tagName; ?>";
		
		$.post("removeTag.php", { name: name, tag: tag }, onModifyTag, "json");

		return false;
	});

	$("#itemListContent li a.tag").livequery('click', function(event) {

		var itemName = $(this).attr("href").split("/").pop();
		var tagName = "<?= $tagName; ?>";
		
		$.post("tagItem.php", { tagName: tagName, itemName: itemName }, onModifyItem, "json");

		return false;
	});

	$("#itemListContent li a.untag").livequery('click', function(event) {

		var itemName = $(this).attr("href").split("/").pop();
		var tagName = "<?= $tagName; ?>";
		
		$.post("untagItem.php", { tagName: tagName, itemName: itemName }, onModifyItem, "json");

		return false;
	});

	function onModifyTag(result)
	{
		if (result.value)
		{
			reloadTagList();
		}
		else
		{
			alert(result.message);
		}
	}
	
	function onModifyItem(result)
	{
		if (result.value)
		{
			reloadItemList();
		}
		else
		{
			 alert(result.message);
		}
	}
	
	function reloadTagList()
	{
		$.ajax({
			url: "listTags.php?tag=<?= $tagName ?>",
			cache: false,
			success: function(data){
				$("#tagListContent").html(data);
			}
		});
	}

	function reloadItemList()
	{
		$.ajax({
			url: "listItemsByTag.php?tag=<?= (isset($_GET["taggable"]) && !empty($_GET["taggable"]))? $tagName . "&taggable=" . $_GET["taggable"] : $tagName ?>",
			cache: false,
			success: function(data){
				$("#itemListContent").html(data);
			}
		});
	}
	
}); 

//-->
</script>

</head>

<body>

<div id="container">

<div id="navigation">

</div><!-- navigation -->

<hr />

<div id="tagListContent">

<?php

include_once("listTags.php");

?>

</div><!-- tagListContent -->

<hr />

<div id="createTagContent">

<form id="myForm" action="createTag.php" method="post"> 
    Name: <input type="text" name="name" /> 
	<input type="hidden" name="tag" value="<?= $tagName; ?>" /> 
	<input type="submit" value="Create tag" /> 
</form>

</div><!-- createTagContent -->

<hr />

<div id="itemListContent">

<?php

if (isset($tagName))
{
	include_once("listItemsByTag.php");
}

?>

</div><!-- itemListContent -->

<hr />

<div id="footer">

</div><!-- footer -->

</div><!-- container -->

</body>
</html>